using System.Collections.Generic;
using SqlQueryBuilder.Util;

namespace SqlQueryBuilder.Criterias
{
    /// <summary>
    /// Into Criteria
    /// </summary>
    public class IntoCriteria : ICriteria
    {
        /// <summary>
        /// Gets or sets the into clauses.
        /// </summary>
        /// <value>The into clauses.</value>
        public List<string> intoClauses { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="IntoCriteria"/> class.
        /// </summary>
        public IntoCriteria()
        {
            intoClauses = new List<string>();
        }

        /// <summary>
        /// Builds the criteria.
        /// </summary>
        /// <returns></returns>
        public string BuildCriteria()
        {
            return intoClauses.Count > 0 ?
                string.Format("({0}) ", GenerateIntoClause()):
                string.Empty;
        }

        /// <summary>
        /// Generates the into clause.
        /// </summary>
        /// <returns></returns>
        private string GenerateIntoClause()
        {
            bool firstClause = true;
            string update = string.Empty;
            foreach (string intoClause in intoClauses)
            {
                if (firstClause)
                    firstClause = false;
                else
                    update = SqlMultipleFields.AddSeparator(update);

                update += intoClause;
            }
            return update;
        }
    }
}